<template>
  <div class="production-line-detail">
    <el-card class="detail-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span class="title">产线详情</span>
          <el-button type="primary" @click="goBack">返回列表</el-button>
        </div>
      </template>

      <div class="detail-content" v-if="productionLineData">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="产线编码">
            {{ productionLineData.lineCode }}
          </el-descriptions-item>
          <el-descriptions-item label="产线名称">
            {{ productionLineData.lineName }}
          </el-descriptions-item>
          <el-descriptions-item label="所属工厂">
            {{ productionLineData.factoryName }}
          </el-descriptions-item>
          <el-descriptions-item label="所属车间">
            {{ productionLineData.workshopName }}
          </el-descriptions-item>
          <el-descriptions-item label="产线类型">
            <el-tag>{{ formatLineType(productionLineData.lineType) }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="productionLineData.status === 'ENABLED' ? 'success' : 'danger'">
              {{ productionLineData.status === 'ENABLED' ? '启用' : '停用' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="位置">
            {{ productionLineData.location }}
          </el-descriptions-item>
          <el-descriptions-item label="最大设备数">
            {{ productionLineData.maxDeviceCapacity || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="支持产品类型" :span="2">
            {{ productionLineData.supportedProductTypes || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="描述" :span="2">
            {{ productionLineData.description || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ productionLineData.createTime }}
          </el-descriptions-item>
          <el-descriptions-item label="更新时间">
            {{ productionLineData.updateTime }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="ProductionLineDetail">
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { getProductionLineById, type ProductionLine } from "@/api/modules/productionLine";

const route = useRoute();
const router = useRouter();
const loading = ref(false);
const productionLineData = ref<ProductionLine.ProductionLineListItem | null>(null);

// 格式化产线类型
const formatLineType = (type: string) => {
  const types: Record<string, string> = {
    ASSEMBLY: "装配线",
    TESTING: "测试线",
    PACKAGING: "包装线",
    INSPECTION: "检验线"
  };
  return types[type] || type;
};

// 获取产线详情
const getProductionLineDetail = async () => {
  const id = route.params.id as string;
  if (!id) {
    ElMessage.error("产线ID不能为空");
    goBack();
    return;
  }

  loading.value = true;
  try {
    const res = await getProductionLineById(id);
    productionLineData.value = res.data;
  } catch (error) {
    ElMessage.error("获取产线详情失败");
    console.error(error);
  } finally {
    loading.value = false;
  }
};

// 返回列表
const goBack = () => {
  router.push("/productionLine");
};

onMounted(() => {
  getProductionLineDetail();
});
</script>

<style scoped>
.production-line-detail {
  padding: 20px;
}

.detail-card {
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: 600;
}

.detail-content {
  padding: 20px 0;
}

:deep(.el-descriptions__label) {
  font-weight: 600;
  width: 120px;
}
</style>
